package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
 * @Author tanxinshi
 * @Date 2020/3/9 17:24
 * @Version v1.0
 * @Description 玩
 */
@Service
@Slf4j
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleDao roleDao;

    @Override
    @Transactional
    public void add(Role role, Integer[] permissionIds,Integer[] menuIds) {
        //1. 添加角色对象， 回显主键id
        roleDao.add(role);
        //2. 维护中间表的数据（角色和权限的关系）
        if (role.getId() != null && permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                roleDao.set(role.getId(), permissionId);
            }
        }
        if (role.getId() != null && menuIds != null && menuIds.length > 0){
            for (Integer menuId : menuIds) {
                roleDao.set1(role.getId(),menuId);
            }
        } else {
            throw new RuntimeException("失败");
        }
    }

    @Override
    @Transactional
    public void edit(Role role, Integer[] permissionIds) {
        roleDao.edit(role);
//2. 维护中间表的数据（角色和权限的关系）
        roleDao.delRelation(role.getId());
        if (role.getId() != null && permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                roleDao.set(role.getId(), permissionId);
            }
        }
    }

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Role> page = roleDao.findByCondition(queryPageBean.getQueryString());
        log.debug("page{" + page + "}");
        return new PageResult(page.getTotal(), page);
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }



    @Override
    public List<Integer> findPermissionIdsById(Integer id) {
        return roleDao.findPermissionIdsById(id);
    }

    @Override
    public List<Integer> findMenuById(Integer id) {
        return roleDao.findMenuById(id);
    }





    /**
     * @Author tanxinshi
     * @Date 2020/3/7 18:31
     * @Version v1.0
     * @Description 删除
     */
    @Override
    @Transactional
    public void delById(Integer id) {
        long count = roleDao.findCountById(id);
        if (count > 0) {
            throw new RuntimeException("该检查组被套餐关联，不能删除!!");
        }
        //删除关系
        roleDao.delRelation(id);
        roleDao.delRelationByMenu(id);
        //删除检查组
        roleDao.delById(id);
    }
}
